package servlet.admin.music;

import pojo.Songs;
import service.MusicMngService;
import service.impl.MusicMngServiceImpl;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 作者：杨晋
 */

@WebServlet(name = "MusicListServlet", value = "/admin/musicListServlet")
public class MusicListServlet extends HttpServlet {
    private MusicMngService MusicMngService = new MusicMngServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer startIndex = Integer.valueOf(request.getParameter("startIndex"));
        Integer targetTotal = Integer.valueOf(request.getParameter("targetTotal"));
        Integer pageSize = targetTotal;  // 每页显示的条目数

        // 获取所有音乐信息
        List<Songs> songsList = MusicMngService.selectMusicList(startIndex, targetTotal);

        // 获取总歌曲数
        Integer totalSongCount = MusicMngService.getTotalSongCount();  // 返回数据库中的歌曲总数
        Integer totalSongPages = (int) Math.ceil(totalSongCount / (double) pageSize);

        request.getSession().setAttribute("songList", songsList);
        request.getSession().setAttribute("totalSongCountM", totalSongCount);
        request.getSession().setAttribute("totalPagesM", totalSongPages);
        request.getSession().setAttribute("targetTotalM", targetTotal);
        request.getSession().setAttribute("currentPageM", (startIndex / targetTotal) + 1);  // 当前页码，假设startIndex从0开始

        // 转发请求到 userMng.jsp
        RequestDispatcher dispatcher = request.getRequestDispatcher("/admin/musicMng.jsp");
        dispatcher.forward(request, response);
    }

}